/*******************************************************************************
 * Copyright (c) 2018, 2018 Grimm
 ******************************************************************************/
package com.alex.java8_01;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

import org.junit.Test;

/**
 * @Author alex
 * @Created Jan 30, 2018 9:31:29 PM
 * @Description
 */
public class TestLambda {

	// 原来的匿名内部类
	@Test
	public void test1() {
		Comparator<Integer> com = new Comparator<Integer>() {

			@Override
			public int compare(Integer o1, Integer o2) {
				return o1.compareTo(o2);
			}
		};

		TreeSet<Integer> tSet = new TreeSet<>(com);
	}

	// Lambda表达式
	@Test
	public void test2() {
		Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
		TreeSet<Integer> tSet = new TreeSet<>(com);
	}

	List<Employee> employees = Arrays.asList(new Employee("张三", 20, 8000), new Employee("李四", 33, 20000), new Employee("王五", 28, 18000),
			new Employee("赵六", 15, 4000), new Employee("田七", 22, 6000), new Employee("牛八", 25, 7000));

	// 需求：筛选出员工中年龄大于30的员工
	@Test
	public void test3() {
		List<Employee> filterEmployeeByAge = filterEmployeeByAge(employees);
		for (Employee employee : filterEmployeeByAge) {
			System.out.println(employee.toString());
		}
	}

	private List<Employee> filterEmployeeByAge(List<Employee> list) {
		List<Employee> employees = new ArrayList<Employee>();
		for (Employee em : list) {
			if (em.getAge() > 25) {
				employees.add(em);
			}
		}
		return employees;
	}

	// 又加一条需求：筛选出工资大于5000的员工
	@Test
	public void test4() {
		List<Employee> filterEmployeeByAge = filterEmployeeBySalary(employees);
		for (Employee employee : filterEmployeeByAge) {
			System.out.println(employee.toString());
		}
	}

	private List<Employee> filterEmployeeBySalary(List<Employee> list) {
		List<Employee> employees = new ArrayList<Employee>();
		for (Employee em : list) {
			if (em.getSalary() >= 8000) {
				employees.add(em);
			}
		}
		return employees;
	}

	// 如此这样下去，相当于每增加一个需求，就需要新增一个方法。代码很冗余
	// 优化方式一：策略设计模式，增加一个接口
	@Test
	public void test5() {
		List<Employee> filterEmployeeByAge = filterEmployee(employees, new FilterEmployeeByAge());
		for (Employee emp : filterEmployeeByAge) {
			System.out.println(emp.toString());
		}

		System.out.println("-------------------------------");

		List<Employee> filterEmployee = filterEmployee(employees, new FilterEmployeeBySalary());
		for (Employee emp : filterEmployee) {
			System.out.println(emp.toString());
		}
	}

	private List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp) {
		List<Employee> emps = new ArrayList<Employee>();
		for (Employee emp : list) {
			if (mp.test(emp)) {
				emps.add(emp);
			}
		}
		return emps;
	}

	// 优化方式二：匿名内部类
	@Test
	public void test6() {
		MyPredicate<Employee> mPredicate = new MyPredicate<Employee>() {

			@Override
			public boolean test(Employee t) {
				return t.getSalary() >= 8000;
			}
		};

		List<Employee> filterEmployeeByAge = filterEmployee(employees, mPredicate);
		for (Employee emp : filterEmployeeByAge) {
			System.out.println(emp.toString());
		}
	}

	// 优化方式三：Lambda表达式
	@Test
	public void test7() {
		List<Employee> emps = filterEmployee(employees, (e) -> e.getAge() > 25);
		emps.forEach(System.out::println);
		emps.forEach((e) -> System.out.println(e));
	}

	// 优化方式四：Stream API
	@Test
	public void test8() {
		employees.stream().filter((e) -> e.getAge() > 25).forEach(System.out::println);
	}
}
